package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.request.BorrowPageRequest;
import com.example.springboot.entity.Borrow;
import com.example.springboot.entity.Returnbook;
import com.example.springboot.service.IBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController                     //指明这是一个接口，所有数据都是Json格式的
@RequestMapping("/borrow")         //后端父级路由
public class BorrowController {

    @Autowired
    private IBorrowService borrowService;

    /**
     * 修改信息
     */
    @PutMapping("/update")
    public Result update(@RequestBody Borrow borrow) {
        borrowService.update(borrow);
        return Result.success();
    }

    /**
     * 根据id获取数据
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Borrow borrow = borrowService.getById(id);
        return Result.success(borrow);
    }

    /**
     * 根据id删除数据
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        borrowService.deleteById(id);
        return Result.success();
    }

    /**
     * 新增接口
     */
    @PostMapping("/save")
    public Result save(@RequestBody Borrow borrow) {
        borrowService.save(borrow);
        return Result.success();
    }


    /**
     * 查询所有数据
     */
    @GetMapping("/list")
    public Result list() {
        return Result.success(borrowService.list());
    }

    /**
     * 分页模糊查询
     */
    @GetMapping("/page")
    public Result page(BorrowPageRequest pageRequest) {
        return Result.success(borrowService.page(pageRequest));
    }
    /**
     * 还书分页模糊查询
     */
    @GetMapping("/pageReturn")
    public Result pageReturn(BorrowPageRequest pageRequest) {
        return Result.success(borrowService.pageReturnbook(pageRequest));
    }
    /**
     * 还书新增接口
     */
    @PostMapping("/saveReturn")
    public Result saveReturn(@RequestBody Returnbook returnbook) {
        borrowService.saveReturn(returnbook);
        return Result.success();
    }
    /**
     * 根据id删除数据
     */
    @DeleteMapping("/deleteReturn/{id}")
    public Result deleteReturnById(@PathVariable Integer id) {
        borrowService.deleteReturnById(id);
        return Result.success();
    }
    /**
     * 借书批量删除数据
     */
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        borrowService.deleteBatch(ids);
        return Result.success();
    }
    /**
     * 还书批量删除数据
     */
    @PostMapping("/deleteReturnBatch")
    public Result deleteReturnBatch(@RequestBody List<Integer> ids){
        borrowService.deleteReturnBatch(ids);
        return Result.success();
    }

    //  timeRange: week month month2 month3
    @GetMapping("/lineCharts/{timeRange}")
    public Result lineCharts(@PathVariable String timeRange) {
        return Result.success(borrowService.getCountByTimeRange(timeRange));
    }
}
